<?php

namespace App\Console\Commands\customer;

use App\Events\Customer\CustomerSaved;
use App\model\customer\Customer;
use App\model\customer\CustomerMobile;
use App\Notifications\WechatNotification;
use App\Toutiao;
use App\ToutiaoLead;
use App\User;
use App\UserProject;
use Carbon\Carbon;


use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Foundation\Auth\User as AuthUser;

class NewJinriCommand extends Command
{


    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'jinri';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';
    protected $client;
    protected $customerRepository;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(Client $client)
    {
        $this->client = $client;
        parent::__construct();

    }


//头条获取数据接口

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $toutiao = Toutiao::where('is_display', 0)->get();
        foreach ($toutiao as $item) {
            $config = $item;

            $host = "https://feiyu.oceanengine.com/";
            $api = "/crm/v2/openapi/pull-clues/";
            $date_s = Carbon::today()->addDays(-2)->toDateString();
            $date_e = Carbon::tomorrow()->toDateString();
            $now = time();
            $start_date = $date_s;
            $end_date = $date_e;
            $source_data = $api . '?start_time=' . $start_date . '&end_time=' . $end_date . ' ' . $now;
            $key = $config['key'];
            $tureOrFalse = true;
            $page = 1;
            while ($tureOrFalse) {

                $result = $this->client->get('https://feiyu.oceanengine.com/crm/v2/openapi/pull-clues/', [
                    'headers' => [
                        'signature' => base64_encode(hash_hmac('sha256', $source_data, $key)),
                        'timestamp' => $now,
                        'Access-Token' => $config['token'],
                    ],
                    'query' => [
                        'start_time' => $start_date,
                        'end_time' => $end_date,
                        'page' => $page,
                        'page_size' => 100,
                    ]
                ]);
                $page++;
                $app = json_decode($result->getBody()->getContents(), true);

                if (!empty($app['data'])) {

                    foreach ($app['data'] as $v) {

                        $toutiaolead = ToutiaoLead::whereClueId($v['clue_id'])->first();

                        if ($toutiaolead && $toutiaolead->customer_id) {
                            continue; //进入下次循环
                        }
                        if (!$toutiaolead) {
                            $v['token'] = $config['token'];
                            $v['key'] = $config['key'];
                            $toutiaolead = ToutiaoLead::create($v);
                            $this->info($toutiao->toJson());
                        }

                        $customer = new Customer();
                        $customer->name = $v['name'];
                        $customer->telephone = $v['telphone'];
                        $customer->source_id = 2;
                        $customer->big_part_id = $item['big_part_id'];
                        $customer->save();
                        $customerMobile = new CustomerMobile();
                        $customerMobile->mobile = $customer->telephone;
                        $customerMobile->customer_id = $customer->customer_id;
                        $customerMobile->save();
                        event(new CustomerSaved($customer));
                        $toutiaolead->customer_id = $customer->customer_id;
                        $toutiaolead->save();
                        //推送
                        \Log::info('新增线索', $customer->toArray());
                        $users = User::permission('allot_group')
                            ->get();
                        if ($users->isNotEmpty()) {
                            foreach ($users as $item) {
                                $item->notify(new   WechatNotification($customer));
                            }
                        }
                    }

                } else {
                    $tureOrFalse = false;
                }
            }
        }
    }
}
